//
//  HEReturnLogisticsViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/16.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEReturnLogisticsViewController.h"
#import "HERetrunLogGoodsCell.h"
#import "HERetrunLogCodeCell.h"
#import "ZFScanViewController.h"

@interface HEReturnLogisticsViewController ()<UITableViewDelegate, UITableViewDataSource, HERetrunLogCodeCellDelegate>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) NSMutableArray *sections;
@property (nonatomic, strong) GDFormModel *formModel;

@end

@implementation HEReturnLogisticsViewController

 
- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"退货物流";
    
    // 总段
    _sections = [NSMutableArray array];
 
    [self.view addSubview:self.tableView];
    [self setupFooterView];
    
    [self initCell];
    
 }

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (void)setupFooterView {
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    footerView.backgroundColor = KWhiteColor;
    
    // 保存按钮
    QMUIFillButton *saveBtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    saveBtn.titleLabel.font = DYBlodFont(16);
    [saveBtn setBackgroundColor:CNavBgColor];
    [saveBtn setTitle:@"提 交" forState:UIControlStateNormal];
    [footerView addSubview:saveBtn];
    [saveBtn addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
    
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(footerView).mas_offset(30);
        make.left.equalTo(footerView).mas_offset(50);
        make.right.equalTo(footerView).mas_offset(-50);
        make.height.mas_offset(40);
    }];
    
    self.tableView.tableFooterView = footerView;
}

- (void)saveClick {
    
    if ([DYTools isBlankString:self.formModel.as_logCode]) {
        [Dialog toastBottom:@"请输入快递编号"];
        return;
    }
    
    NSMutableDictionary *params = @{}.mutableCopy;
    [params setValue:[NSNumber numberWithInteger:_orderItems.firstObject.id] forKey:@"orderItemId"];
    [params setValue:self.formModel.as_logCode forKey:@"deliverySn"];

    [WOAFNetWorkingHandle POSTWithBaseUrlByKeyValue:KServerUrl suffixUrl:Ksendreship parameters:params success:^(id  _Nonnull responseObject) {
        
        HEResponse *response = [HEResponse mj_objectWithKeyValues:responseObject];
        if (response.success) {
            [Dialog toastBottom:@"提交成功"];
            if (self.backBlock) {
                self.backBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError * _Nonnull error) {

    }];
}
 
#pragma mark - HERetrunLogCodeCellDelegate
- (void)qrcodeWithCell:(HERetrunLogCodeCell *)cell {
    ZFScanViewController * vc = [[ZFScanViewController alloc] init];
    vc.returnScanBarCodeValue = ^(NSString * barCodeString){
        self.formModel.as_logCode = barCodeString;
        [self.tableView reloadData];
    };
    
     [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark -

- (void)initCell {
    
    _sections = @[].mutableCopy;
    self.formModel.od_itemList = [NSMutableArray arrayWithArray:_orderItems];
    
    NSMutableArray *classArr = [NSMutableArray array];
 
    // 商品
    GDFiterCellModel *goodModel = [GDFiterCellModel new];
    goodModel.cellKlass = HERetrunLogGoodsCell.class;
    goodModel.cellType = GDFiterCellTypeOrderGoods;
    
    // 扫码快递编号
    GDFiterCellModel *qrcodeModel = [GDFiterCellModel new];
    qrcodeModel.cellKlass = HERetrunLogCodeCell.class;
    
    
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[goodModel, qrcodeModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];
    
    for (GDFiterCellModel *model in classArr) {
        NSMutableArray *rows = @[].mutableCopy;

        if (model.cellType == GDFiterCellTypeOrderGoods) {
         
            for (HEOrderItemModel *item in _orderItems ) {
                [rows addObject: model];
            }
        }else {
            [rows addObject:model];
        }
        [_sections addObject:rows];
    }
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *rows = _sections[section];
    return rows.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *rows = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = rows[indexPath.row];
    
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *rows = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = rows[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
        [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

@end
